package com.heatup.apt.management.task;

import java.io.Serializable;
import java.util.Date;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.heatup.apt.common.constants.TokenConstants;
import com.heatup.apt.common.dao.TokenMapper;
import com.heatup.apt.common.dao.redis.BaseRedisDao;
import com.heatup.apt.common.util.TemplateMsgUtil;
import com.heatup.apt.management.util.ManageConfigPropertiesUtil;
import com.heatup.apt.model.weixin.Token;

public class InitTokenListener implements ServletContextListener {

	private Logger logger = LoggerFactory.getLogger(InitTokenListener.class);
	private TokenMapper tokenMapper;
	private WebApplicationContext springContext;
	private RedisTemplate<Serializable, Serializable> redisTemplate;
	

	@Override
	public void contextInitialized(ServletContextEvent event) {

		logger.info("==>启动更新tokenListener InitTokenListener webcontaner is contextInitialized");
		String TOKEN_UPDATE_FLAG = ManageConfigPropertiesUtil.getString("token_update_flag").trim();
		Token token =null;
		try {
			//1表示启动需要重新更新token
			if ("1".equals(TOKEN_UPDATE_FLAG)) {
				springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
				tokenMapper = (TokenMapper) springContext.getBean("tokenMapper");
				redisTemplate= (RedisTemplate<Serializable, Serializable>) springContext.getBean("redisTemplate");
				String access_token = TemplateMsgUtil.getAccessToken();
				logger.info(access_token);
				token = tokenMapper.selectTokenByKey(TokenConstants.ACCESS_TOKEN_KEY);
				if (token != null) {
					token.setToken(access_token);
					token.setCreateTime(new Date());
					tokenMapper.updateByPrimaryKey(token);
				} else {
					token = new Token();
					token.setKey(TokenConstants.ACCESS_TOKEN_KEY);
					token.setCreateTime(new Date());
					token.setVaildTime("7200");
					token.setToken(access_token);
					tokenMapper.insertToken(token);
				}
				// 插入redis缓存
				try {
					if (token != null) {
						BaseRedisDao<Token> baseRedisDao = new BaseRedisDao<Token>(redisTemplate);
						baseRedisDao.setToken_vaild_time(5400);
						baseRedisDao.save(TokenConstants.ACCESS_TOKEN_KEY, token);
					}
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("==>保存redis失败 InitTokenListener method is contextInitialized save redis access token error!"+ e.toString() + e.getCause());
				}	
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error("==>启动更新tokenListener异常： InitTokenListener method is contextInitialized error!" + ex.toString() + ex.getCause());
		}
		logger.info("==>启动更新tokenListener完成。 InitTokenListener contextInitialized method run end !");
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		logger.info("InitTokenListener webcontaner is destoryed!");

	}

}
